package com.xd.study.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.xd.study.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DGF on 2015/4/22.
 */
public class GridViewActivity extends Activity implements AdapterView.OnItemClickListener {

    private GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview);

        int icon = R.drawable.item;

        String[] funcNames = {"up_refresh_lv",
                              "down_refresh_lv","button","gesture_builder",
                              "gesture_overlay_view", "gesture_detector","sys_service",
                              "service","broadcast","content_provider",
                              "data_storage","menu","notification",
                              "dialog","toast","layout_optimize",
                              "seekbar", "gallery", "scrollview",
                              "viewflipper", "viewpager", "fragment",
                              "webview", "progressbar", "spinner", "datepicker",
                              "listview", "marquee_text", "autocomplete",
                              "toggle", "checkbox", "radio",
                              "table", "frame", "relative"};

        List<Map<String, Object>> data = new ArrayList<>();
        for (int i=0;i<funcNames.length;i++){
            Map<String, Object> item = new HashMap<>();
            item.put("image", icon);
            item.put("text", funcNames[i]);
            data.add(item);
        }

        String[] from = {"image", "text"};
        int[] to = {R.id.gridview_item_pic, R.id.gridview_item_text};
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.gridview_item, from, to);

        gridView = (GridView)this.findViewById(R.id.gridView);
        gridView.setAdapter(simpleAdapter);
        gridView.setOnItemClickListener(this);
    }

    /**
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        /**
         * GridView.GetChildCount() -- 当前视窗中的子控件数目
         * GridView.GetCound() -- 所有子控件数目，大于等于GetChildCount
         */
        int index = gridView.getCount() - position;
        Intent intent = null;
        switch (index){
            case 35:
                intent = new Intent(GridViewActivity.this, ListViewUpLoadActivity.class);
                break;
            case 34:
                intent = new Intent(GridViewActivity.this, ListViewDownRefreshActivity.class);
                break;
            case 33:
                intent = new Intent(GridViewActivity.this, ButtonActivity.class);
                break;
            case 32:
                intent = new Intent(GridViewActivity.this, GestureBuilderActivity.class);
                break;
            case 31:
                intent = new Intent(GridViewActivity.this, GestureOverlayViewActivity.class);
                break;
            case 30:
                intent = new Intent(GridViewActivity.this, GestureDetectorActivity.class);
                break;
            case 29:
                intent = new Intent(GridViewActivity.this, SystemServiceActivity.class);
                break;
            case 28:
                intent = new Intent(GridViewActivity.this, ServiceActivity.class);
                break;
            case 27:
                intent = new Intent(GridViewActivity.this, BroadcastActivity.class);
                break;
            case 26:
                intent = new Intent(GridViewActivity.this, ContentProviderActivity.class);
                break;
            case 25:
                intent = new Intent(GridViewActivity.this, DataStorageActivity.class);
                break;
            case 24:
                intent = new Intent(GridViewActivity.this, MenuActivity.class);
                break;
            case 23:
                intent = new Intent(GridViewActivity.this, NotificationActivity.class);
                break;
            case 22:
                intent = new Intent(GridViewActivity.this, DialogActivity.class);
                break;
            case 21:
                intent = new Intent(GridViewActivity.this, ToastActivity.class);
                break;
            case 20:
                intent = new Intent(GridViewActivity.this, LayoutOptimizeActivity.class);
                break;
            case 19:
                intent = new Intent(GridViewActivity.this, SeekBarActivity.class);
                break;
            case 18:
                intent = new Intent(GridViewActivity.this, GalleryActivity.class);
                break;
            case 17:
                intent = new Intent(GridViewActivity.this, ScrollViewActivity.class);
                break;
            case 16:
                intent = new Intent(GridViewActivity.this, ViewFlipperActivity.class);
                break;
            case 15:
                intent = new Intent(GridViewActivity.this, ViewPagerActivity.class);
                break;
            case 14:
                intent = new Intent(GridViewActivity.this, FragmentTestActivity.class);
                break;
            case 13:
                intent = new Intent(GridViewActivity.this, WebViewActivity.class);
                break;
            case 12:
                intent = new Intent(GridViewActivity.this, ProgressBarActivity.class);
                break;
            case 11:
                intent = new Intent(GridViewActivity.this, SpinnerActivity.class);
                break;
            case 10:
                intent = new Intent(GridViewActivity.this, DatePickerActivity.class);
                break;
            case 9:
                intent = new Intent(GridViewActivity.this, ListViewActivity.class);
                break;
            case 8:
                intent = new Intent(GridViewActivity.this, MarqueeTextActivity.class);
                break;
            case 7:
                intent = new Intent(GridViewActivity.this, AutoCompleteTextViewActivity.class);
                break;
            case 6:
                intent = new Intent(GridViewActivity.this, ToggleButtonActivity.class);
                break;
            case 5:
                intent = new Intent(GridViewActivity.this, CheckBoxActivity.class);
                break;
            case 4:
                intent = new Intent(GridViewActivity.this, RadioButtonActivity.class);
                break;
            case 3:
                intent = new Intent(GridViewActivity.this, TableLayoutActivity.class);
                break;
            case 2:
                intent = new Intent(GridViewActivity.this, FrameLayoutActivity.class);
                break;
            case 1:
                intent = new Intent(GridViewActivity.this, RelativeLayoutActivity.class);
                break;
        }
        GridViewActivity.this.startActivity(intent);
    }
}
